// Utility standard header   -*-c++-*-
// Copyright 2003-2010 IAR Systems AB. 
#ifndef _UTILITY_
#define _UTILITY_

#ifndef _SYSTEM_BUILD
#pragma system_include
#endif

#include <iosfwd>
#include <iutility>

_STD_BEGIN
#pragma language = save
#pragma language = extended

// TEMPLATE FUNCTION swap (from <algorithm>)
#pragma basic_template_matching
template<class _Ty> inline
void swap(_Ty& _Left, _Ty& _Right)
{			 // exchange values stored at _Left and _Right
  typename _TypeUtil::_StripMemory<_Ty>::_Type _Tmp = _Left;
  _Left = _Right, _Right = _Tmp; }

// TEMPLATE STRUCT pair
template<class _Ty1, class _Ty2> 
struct __memory_of(typename _TypeUtil::_CopyMemory<_Ty1,_Ty2>::_Type) pair 
{				// store a pair of values
  typedef pair<_Ty1, _Ty2> _Myt;

  typedef typename _TypeUtil::_StripMemory<_Ty1>::_Type first_type;
  typedef typename _TypeUtil::_StripMemory<_Ty2>::_Type second_type; 

  typedef typename _TypeUtil::_ParameterType<_Ty1>::_Type _ParamTy1;
  typedef typename _TypeUtil::_ParameterType<_Ty2>::_Type _ParamTy2;

  pair()
    : first(), second()         // Default construct
    {
      // If default construction didn't do anything, initialize to achieve
      // the same effect as construction from default constructed temps,
      // since temps cannot always be constructed in auto memory.
      _ClassUtil::_Init<_Ty1>::_Doit(&first);
      _ClassUtil::_Init<_Ty2>::_Doit(&second);
    }

  explicit pair(_ParamTy1 _Val1)
    : first(_Val1), second()
    {}				// second is default constructed

  pair(_ParamTy1 _Val1, _ParamTy2 _Val2)
    : first(_Val1), second(_Val2)
    {}				// construct from specified values

  template<class _Other1, class _Other2>
    pair(const pair<_Other1, _Other2>& _Right)
    : first(_Right.first), second(_Right.second)
    {}				// construct from compatible pair

  void swap(_Myt& _Right)
    {				// exchange contents with _Right
      using _STD swap;
      /*_STD*/ swap(first, _Right.first);
      /*_STD*/ swap(second, _Right.second); 
    }

  first_type first;		// the first stored value
  second_type second;		// the second stored value
};

// pair TEMPLATE OPERATORS
template<class _Ty1,
         class _Ty2> inline
void swap(pair<_Ty1, _Ty2>& _Left, pair<_Ty1, _Ty2>& _Right)
{       // swap _Left and _Right pairs
  _Left.swap(_Right); 
}

template<class _Ty1,
         class _Ty2> inline
bool operator==(const pair<_Ty1, _Ty2>& _Left,
                const pair<_Ty1, _Ty2>& _Right)
{       // test for pair equality
  return (_Left.first == _Right.first && _Left.second == _Right.second); 
}

template<class _Ty1,
         class _Ty2> inline
bool operator!=(const pair<_Ty1, _Ty2>& _Left,
                const pair<_Ty1, _Ty2>& _Right)
{       // test for pair inequality
  return (!(_Left == _Right)); 
}

template<class _Ty1,
         class _Ty2> inline
bool operator<(const pair<_Ty1, _Ty2>& _Left,
               const pair<_Ty1, _Ty2>& _Right)
{       // test if _Left < _Right for pairs
  return (_Left.first < _Right.first ||
          !(_Right.first < _Left.first) && _Left.second < _Right.second); 
}

template<class _Ty1,
         class _Ty2> inline
bool operator>(const pair<_Ty1, _Ty2>& _Left,
               const pair<_Ty1, _Ty2>& _Right)
{       // test if _Left > _Right for pairs
  return (_Right < _Left); 
}

template<class _Ty1,
         class _Ty2> inline
bool operator<=(const pair<_Ty1, _Ty2>& _Left,
                const pair<_Ty1, _Ty2>& _Right)
{       // test if _Left <= _Right for pairs
  return (!(_Right < _Left)); 
}

template<class _Ty1,
         class _Ty2> inline
bool operator>=(const pair<_Ty1, _Ty2>& _Left,
                const pair<_Ty1, _Ty2>& _Right)
{       // test if _Left >= _Right for pairs
  return (!(_Left < _Right)); 
}

template<class _Ty1,
         class _Ty2> inline
pair<_Ty1, _Ty2> make_pair(_Ty1 _Val1, _Ty2 _Val2)
{       // return pair composed from arguments
  return (pair<_Ty1, _Ty2>(_Val1, _Val2)); 
}

#if _HAS_NAMESPACE

// TEMPLATE OPERATORS
namespace rel_ops
{       // nested namespace to hide relational operators from std
  template<class _Ty> inline
  bool operator!=(const _Ty& _Left, const _Ty& _Right)
  {       // test for inequality, in terms of equality
    return (!(_Left == _Right)); 
  }

  template<class _Ty> inline
  bool operator>(const _Ty& _Left, const _Ty& _Right)
  {       // test if _Left > _Right, in terms of operator<
    return (_Right < _Left); 
  }

  template<class _Ty> inline
  bool operator<=(const _Ty& _Left, const _Ty& _Right)
  {       // test if _Left <= _Right, in terms of operator<
    return (!(_Right < _Left)); 
  }

  template<class _Ty> inline
  bool operator>=(const _Ty& _Left, const _Ty& _Right)
  {       // test if _Left >= _Right, in terms of operator<
    return (!(_Left < _Right)); 
  }
}
#else
#endif
#pragma language = restore
_STD_END
#endif /* _UTILITY_ */

/*
 * This file is derived from software bearing the following
 * restrictions:
 *
 * Copyright (c) 1994
 * Hewlett-Packard Company
 *
 * Permission to use, copy, modify, distribute and sell this
 * software and its documentation for any purpose is hereby
 * granted without fee, provided that the above copyright notice
 * appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation.
 * Hewlett-Packard Company makes no representations about the
 * suitability of this software for any purpose. It is provided
 * "as is" without express or implied warranty.
 */

/*
 * Copyright (c) 1992-2009 by P.J. Plauger.  ALL RIGHTS RESERVED.
 * Consult your license regarding permissions and restrictions.
 V5.04:0576 */
